package com.mmz.test.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class JMSReciverMessageGroup2 {
    public static void main(String args[]) throws Exception {
        Session session = null;
        Connection con = null;
        try {
            // 通过工厂模式获取链接
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            con = connectionFactory.createConnection();
            con.start();
            // 创建会话（是否使用事务，消息确认机制）
            session = con.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            // 创建目的地（消息队列名称）
            Destination destination = session.createQueue("my-queue");
            String condition = "JMXGroupId=odds";
            // 创建消费者（消息接收器）
            MessageConsumer consumer = session.createConsumer(destination,condition);
            while(true){
                TextMessage message = (TextMessage) consumer.receive();
                    System.out.println("接收消息：" + message.getText());
                    session.commit();

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
            con.close();
        }
    }
}
